﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.PORk;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.POOK.AddWhitePork
{
    public class AddWhitePorkHandler : IRequestHandler<AddWhitePorkCommand, Result>
    {
        public AddWhitePorkHandler(IBaseRepository<WhiteporkData> whiteporkDataRepository, IMapper mapper)
        {
            WhiteporkDataRepository = whiteporkDataRepository;
            this.mapper = mapper;
        }

        public IBaseRepository<WhiteporkData> WhiteporkDataRepository { get; set; }
        public IMapper mapper { get; set; }
        public async Task<Result> Handle(AddWhitePorkCommand request, CancellationToken cancellationToken)
        {
           var result=new Result() { Code=(int)CommonErrorCode.Added};
            var data = mapper.Map<WhiteporkData>(request);
            if (data != null)
            {
                var list = WhiteporkDataRepository.Add(data);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
